package com.huike.web.controller.clues;

import com.alibaba.excel.EasyExcel;
import com.huike.business.service.TbBusinessService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.dto.TbClueCreateDTO;
import com.huike.clues.domain.dto.TbRuleListDTO;
import com.huike.clues.domain.dto.TbUserGainClueDTO;
import com.huike.clues.domain.vo.FalseClueVo;
import com.huike.clues.domain.vo.PageClueVO;
import com.huike.clues.domain.vo.TbClueExcelVo;
import com.huike.clues.result.ImportClueData;
import com.huike.clues.service.TbClueService;
import com.huike.clues.utils.easyExcel.DemoDataListener;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.BaseException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 线索管理Controller
 *
 * @date 2023-04-02
 */
@RequestMapping("/clues/clue")
@RequiredArgsConstructor
@RestController
@Slf4j
public class TbClueController {

    private final TbClueService service;

    private final TbBusinessService tbBusinessService;

    /**
     * 新增线索管理
     */
    @PostMapping
    public AjaxResult<?> create(@RequestBody TbClueCreateDTO dto) {
        log.info("Get ClueCreateDTO: {}", dto);
        service.createClue(dto);
        return AjaxResult.success();
    }

    /**
     * 批量分配
     *
     * @param dto 传入批量分配的线索id集合以及分配给哪个用户的id
     */
    @PutMapping("/assignment")
    public AjaxResult<?> assignment(@RequestBody TbUserGainClueDTO dto) {
        log.info("Get UserGainClueDTO: {}", dto);
        service.assignment(dto);
        return AjaxResult.success();
    }

    /**
     * 线索转商机
     *
     * @param id 待转为商机的线索id
     */
    @PutMapping("/changeBusiness/{id}")
    public AjaxResult<?> changeBusiness(@PathVariable Long id) {
        log.info("Get clueId: {}", id);
        tbBusinessService.changeBusiness(id);
        return AjaxResult.success();
    }

    /**
     * 伪线索
     *
     * @param id 线索id
     */
    @PutMapping("/false/{id}")
    public AjaxResult<?> falseClue(@PathVariable Long id, @RequestBody FalseClueVo falseClueVo) {
        log.info("Get clueId: {}, FalseClueVo: {}", id, falseClueVo);
        service.falseClue(id, falseClueVo);
        return AjaxResult.success();
    }

    /**
     * 批量捞取
     *
     * @param dto 传入批量捞取的线索id集合以及分配给捞取用户的id
     */
    @PutMapping("/gain")
    public AjaxResult<?> gainClue(@RequestBody TbUserGainClueDTO dto) {
        log.info("Get UserGainClueDTO: {}", dto);
        service.gain(dto);
        return AjaxResult.success();
    }

    /**
     * 更新线索
     */
    @PutMapping
    public AjaxResult<?> update(@RequestBody TbClue clue) {
        log.info("Get TbClue: {}", clue);
        service.updateClue(clue);
        return AjaxResult.success();
    }

    /**
     * 获取线索管理列表
     *
     * @param dto 查询条件
     * @return 返回线索集合
     */
    @GetMapping("/list")
    public TableDataInfo<List<PageClueVO>> list(TbRuleListDTO dto) {
        log.info("Get TbRuleListDTO: {}", dto);
        return service.getPageClue(dto);
    }

    /**
     * 获取线索池
     *
     * @param dto 查询条件
     * @return 返回线索池
     */
    @GetMapping("/pool")
    public TableDataInfo<List<PageClueVO>> getCluePool(TbRuleListDTO dto) {
        log.info("Get TbClue: {}", dto);
        return service.getCluePool(dto);
    }

    /**
     * 根据id获取线索信息
     *
     * @param id 线索id
     * @return 返回线索信息
     */
    @GetMapping("/{id}")
    public AjaxResult<PageClueVO> getInfo(@PathVariable Long id) {
        log.info("Get id: {}", id);
        return AjaxResult.success(service.getInfoById(id));
    }

    /**
     * 上传线索
     */
    @PostMapping("/importData")
    public AjaxResult<ImportClueData> importData(MultipartFile file) {
        DemoDataListener demoDataListener = new DemoDataListener(service);
        try {
            EasyExcel.read(file.getInputStream(), TbClueExcelVo.class, demoDataListener).sheet().doRead();
        } catch (IOException e) {
            throw new BaseException(e.getMessage());
        }
        return AjaxResult.success(demoDataListener.getResultData());
    }

}
